//
//  AppDelegate.swift
//  Alili
//
//  Created by 郑东喜 on 2016/11/29.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit


@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

    
    
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        // 创建window
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = WebView()
        window?.makeKeyAndVisible()
        
        return true
    }
    
    
}

// MARK:- 支付宝支付方法
extension AppDelegate {
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        
        if url.host == "safepay" {
            // 支付跳转支付宝钱包进行支付，处理支付结果
            
            AlipaySDK.defaultService().processOrder(withPaymentResult: url, standbyCallback: {[weak self] (resultDic) in
                
                
                
                    
                    var data = NSDictionary()
                    
                    if let resultStatus = resultDic?["resultStatus"] as? String {
                        

                        if resultStatus == "9000"{
//                            print("OK")
                            data = ["re" : "支付成功"]

                        }else if resultStatus == "8000"{
//                            print("正在处理中")
                            data = ["re" : "正在处理中"]
                            
                        }else if resultStatus == "4000"{
//                            print("订单支付失败")
                            data = ["re" : "订单支付失败"]
                            
                        }else if resultStatus == "6001" {
//                            print("用户中途取消")
                            data = ["re" : "用户中途取消"]
                            
                        } else if resultStatus == "6002" {
//                            print("网络连接出错")
                            data = ["re" : "网络连接出错"]
                        } 
                    }
                    
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "123"), object: self, userInfo: data as? [AnyHashable : Any])
                
//                }

                
            })
            
        }
        return true
    }
    
    
}
